package com.ybkj.daijia.server.service.impl;

import com.ybkj.daijia.Setting;
import com.ybkj.daijia.serverUtils.SettingUtils;
import com.ybkj.daijia.common.HtmlHelper;
import com.ybkj.daijia.common.Pager;
import com.ybkj.daijia.page.Page;
import com.ybkj.daijia.page.Pageable;
import com.ybkj.daijia.page.impl.PageImpl;
import com.ybkj.daijia.page.impl.PageRequest;
import com.ybkj.daijia.server.mapper.AnnouncementMapper;
import com.ybkj.daijia.server.model.Announcement;
import com.ybkj.daijia.server.service.AnnouncementService;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DefaultAnnouncementServiceService implements AnnouncementService {

    @Autowired
    private AnnouncementMapper announcementMapper;
    @Autowired
    private SettingUtils settingUtils;

    @Override
    public Page<Announcement> queryAll() {
        Pageable pageable = new PageRequest(Pager.getPage(), Pager.getLimit());
        List<Announcement> content = announcementMapper
            .selectByCompany(null, Pager.getOffset(), Pager.getLimit());
        for (Announcement announcement : content) {
            String text = HtmlHelper.html2Text(announcement.getContent());
            if (text.length() > 60) {
                text = text.substring(0, 60) + "...";
            }
            announcement.setContent(text);
        }
        Long total = announcementMapper.countByCompany(null);

        return new PageImpl<Announcement>(content, pageable, total);
    }

    @Override
    public Announcement findOne(Long id) {
        return announcementMapper.selectByPrimaryKey(id);
    }

    @Override
    public Announcement create(String title, String content, Long companyId, String operator,
        String companyName) {
        Announcement announcement = new Announcement();
        announcement.setTitle(title);
        announcement.setContent(content);
        announcement.setCompanyId(companyId);
        announcement.setOperator(operator);
        announcement.setCompanyName(companyName);
        announcementMapper.insertAnnouncement(announcement);
        return announcement;
    }

    @Override
    public Page<Announcement> findByCompanyId(Long companyId) {
        Pageable pageable = new PageRequest(Pager.getPage(), Pager.getLimit());
        List<Announcement> content = announcementMapper
            .selectByCompany(companyId, Pager.getOffset(), Pager.getLimit());
        Long total = announcementMapper.countByCompany(companyId);
        for (Announcement announcement : content) {
            String text = HtmlHelper.html2Text(announcement.getContent());
            if (text.length() > 60) {
                text = text.substring(0, 60) + "...";
            }
            announcement.setContent(text);
        }
        return new PageImpl<Announcement>(content, pageable, total);
    }

    @Override
    public int delete(Long id) {
        return announcementMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Page<Map<String, Object>> queryByCompanyId(Long companyId, Integer page, Integer limit) {

        Pageable pageable = new PageRequest(page, limit);
        Setting setting = settingUtils.get();
        List<Announcement> content = announcementMapper
            .selectByCompanyIdOrNull(companyId, page * limit, limit);
        Long total = announcementMapper.countByCompanyIdOrNull(companyId);
        List<Map<String, Object>> dataMapList = new ArrayList<Map<String, Object>>();
        for (Announcement announcement : content) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("id", announcement.getId());
            map.put("title", announcement.getTitle());
            map.put("created", announcement.getCreated().getTime());
            map.put("url",
                setting.getWebUrl() + "/driver/api/Announcement/v4/viewAnnouncement/" + announcement
                    .getId());
            dataMapList.add(map);
        }
        return new PageImpl<Map<String, Object>>(dataMapList, pageable, total);

    }

}

